package com.github.shiqiyue.kibana.translator.zhcn;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.shiqiyue.kibana.translator.zhcn.entity.KibanaResource;
import com.github.shiqiyue.kibana.translator.zhcn.util.PathUtils;
import com.github.shiqiyue.kibana.translator.zhcn.util.StringUtils;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;

/***
 * kibana汉化程序
 * @author wenyao.wu
 * @date 2018/7/25
 */
public class Application {

    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.load(Application.class.getResourceAsStream("/app.properties"));
        String kibanaLocation = properties.getProperty("kibana.location");
        String type = properties.getProperty("translator.type");
        if (StringUtils.isBlank(kibanaLocation)) {
            if (args != null && args.length >= 1) {
                kibanaLocation = args[0];
            }
        }
        if (StringUtils.isBlank(kibanaLocation)) {
            kibanaLocation = PathUtils.getCurrentPath();
        }
        if (!kibanaLocation.endsWith(File.separator)) {
            kibanaLocation = kibanaLocation + File.separator;
        }
        if (StringUtils.isBlank(type)) {
            type = "1";
        }
        System.out.println("kibana地址为:" + kibanaLocation);
        if (type.equals("1")) {
            System.out.println("执行汉化");
        } else if (type.equals("2")) {
            System.out.println("还原汉化");
        }
        if (StringUtils.isBlank(kibanaLocation)) {
            return;
        }
        String json = IOUtils.toString(Application.class.getResourceAsStream("/kibana_resource.json"), StandardCharsets.UTF_8);
        if (StringUtils.isBlank(json)) {
            return;
        }
        List<KibanaResource> kibanaResources = JSON.parseArray(json, KibanaResource.class);
        if (kibanaResources == null || kibanaResources.isEmpty()) {
            return;
        }
        //开始汉化文件
        for (KibanaResource kibanaResource : kibanaResources) {
            String fileName = kibanaLocation + kibanaResource.getPath();
            File file = new File(fileName);
            if (type.equals("1")) {
                Translator.translate(file, kibanaResource.getEn(), kibanaResource.getCn());
            } else if (type.equals("2")) {
                Translator.translate(file, kibanaResource.getCn(), kibanaResource.getEn());
            }
        }
        System.out.println("成功汉化以下文件:");
        for (String successFile : TranslatorContext.successSet) {
            System.out.println(successFile);
        }
        System.out.println("未找到以下文件:");
        for (String notFindFile : TranslatorContext.notFindFileSet) {
            System.out.println(notFindFile);
        }
        System.out.println("汉化完成");
    }
}
